﻿using AutoMapper;
using HamcoWcs.Contract.ContractCommon;
using HamcoWcs.Contract.ContractMes;
using HamcoWcs.Contract.ContractPcon;
using HamcoWcs.Contract.ContractSrm;
using HamcoWcs.DAL;
using HamcoWcs.Entitys.MesMsgQueues;
using HamcoWcs.Models.Common.Enums;
using HamcoWcs.Models.TibcoMessage;
using HamcoWcs.Service.Common;
using System;
using System.Linq;
using System.Xml;

namespace HamcoWcs.Service.Mes.Parsers
{
    /// <summary>
    /// MES获取托盘信息
    /// </summary>
    public class MesMsgParser_GetPalletDataRequest : MesMsgParserBase
    {
        public override void HandleEvent(MesMsgQueue message)
        {
            GetPalletDataMessage msg;
            try
            {
                msg = XmlHelper.Deserialize<GetPalletDataMessage>(message.Content);
                if (msg == null) throw new Exception();
            }
            catch (Exception ex)
            {
                throw new Exception("MesMsgParser->GetPalletDataRequest:解析失败");
            }

            msg.HEADER.MESSAGENAME = "GetPalletDataReply";
            var palletName = msg.Body.PalletName;
            if (string.IsNullOrEmpty(palletName))
            {
                msg.RETURN.RETURNCODE = "0xFF01";
                msg.RETURN.RETURNMESSAGE = "Abnormal mes request content";
                message.Return = XmlHelper.Serialize(msg);
                message.Status = (int)EnumMesMsgStatus.WcsExcuted;
                return;
            }

            //var addr = _locationService.GetAddress(palletName);

            bool isFind = false;

            var loc = _locationService.GetPalletLocation(palletName);
            if (loc != null)
            {
                msg.Body.CurrentPositionName = loc.WcsLocName;
                msg.Body.CurrentZoneName = loc.ZoneName;
                msg.Body.CurrentPositionType = "SHELF";
                msg.Body.CurrentMachineName = loc.ZoneName.Substring(0, 8);
                msg.Body.TransferState = "INSTK";
                msg.Body.PalletLocState = (loc.IsLock || loc.SrmOccupyFlag) ? "BLOCKED" : "UNBLOCKED";
                isFind = true;
            }

            if (!isFind)
            {
                var pcons = _pconService.GetAllPcons();
                var pcon = pcons.FirstOrDefault(x => x.UnitId == palletName);
                if (pcon != null)
                {
                    msg.Body.CurrentPositionName = pcon.PortName;
                    msg.Body.CurrentZoneName = "";
                    msg.Body.CurrentPositionType = "PORT";
                    msg.Body.CurrentMachineName = pcon.MachineName;
                    msg.Body.TransferState = "MOVING";
                    msg.Body.PalletLocState = "";
                    isFind = true;
                }
            }

            if (!isFind)
            {
                var srm = _asrs.Srms.FirstOrDefault(x => x.UnitId == palletName);
                if (srm != null)
                {
                    msg.Body.CurrentPositionName = srm.PortName;
                    msg.Body.CurrentZoneName = "";
                    msg.Body.CurrentPositionType = "PORT";
                    msg.Body.CurrentMachineName = srm.MachineName;
                    msg.Body.TransferState = "MOVING";
                    msg.Body.PalletLocState = "";
                    isFind = true;
                }
            }

            if (!isFind)
            {
                msg.RETURN.RETURNCODE = "40961";
                msg.RETURN.RETURNMESSAGE = "Pallet not found";
                message.Return = XmlHelper.Serialize(msg);
                message.Status = (int)EnumMesMsgStatus.WcsExcuted;
                return;
            }

            message.Return = XmlHelper.Serialize(msg);
            message.Status = (int)EnumMesMsgStatus.WcsExcuted;
        }


        private readonly ILocationService _locationService;
        private readonly IPconBaseService _pconService;
        private readonly IBaseSrmTaskService _srmService;
        private readonly AutomaticStorageInfo _asrs;
        public MesMsgParser_GetPalletDataRequest(ILoggerService loggerService, WcsDbContext wcsDbContext, IMapper mapper, IPconBaseService pconBaseService, ILocationService locationService, IBaseSrmTaskService srmService, AutomaticStorageInfo asrs) : base(loggerService, wcsDbContext, mapper)
        {
            _pconService = pconBaseService;
            _locationService = locationService;
            _srmService = srmService;
            _asrs = asrs;
        }

        public override string EventName => "GETPALLETDATAREQUEST";

    }
}
